package com.erp.service.util.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.protocol.HTTP;

import android.text.TextUtils;
import android.util.Log;


/**
 * 通用返回结果接口
 * 
 * @author ccl
 * 
 */
public class ResponsedResult {

	private final static String TAG = "ResponsedResult";

	protected HttpResponse httpResponse;

	protected HttpUriRequest httpUriRequest;

	public ResponsedResult(HttpResponse httpResponse,
			HttpUriRequest httpUriRequest) {
		this.httpResponse = httpResponse;
		this.httpUriRequest = httpUriRequest;
	}

	/**
	 * 获取结果的响应对象
	 * 
	 * @return
	 */
	public HttpResponse getHttpResponse() {
		return httpResponse;
	}

	public void close() {
		if (httpUriRequest != null)
			httpUriRequest.abort();
	}

	/**
	 * 获取返回的字符串
	 */
	@Override
	public String toString() {
		try {
			String result = retrieveInputStream(httpResponse.getEntity());
			Log.d(TAG, "response:" + result);
			return result;
		} finally {
			if (httpUriRequest != null)
				httpUriRequest.abort();
		}

	}
	
	/**
	 * 获取返回的字符串
	 */
	public String toString(String encode) {
		try {
			String result = retrieveInputStream(httpResponse.getEntity(), encode);
			Log.d(TAG, "response:" + result);
			return result;
		} finally {
			if (httpUriRequest != null)
				httpUriRequest.abort();
		}

	}

	/**
	 * 获取响应的输入流
	 * 
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public InputStream getInputStream() throws IllegalStateException,
			IOException {
		return httpResponse.getEntity().getContent();
	}

	/**
	 * 处理httpResponse信息,返回String
	 * 
	 * @param httpEntity
	 * @return String
	 */
	protected String retrieveInputStream(HttpEntity httpEntity) {
		return retrieveInputStream(httpEntity, getEncoding(httpEntity));
	}
	
	/**
	 * 处理httpResponse信息,返回String
	 * 
	 * @param httpEntity
	 * @return String
	 */
	protected String retrieveInputStream(HttpEntity httpEntity, String encode) {
		int length = (int) httpEntity.getContentLength();
		if (length < 0)
			length = 10000;
		StringBuffer stringBuffer = new StringBuffer(length);
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(
					httpEntity.getContent(), encode/*HTTP.UTF_8*/);
			char buffer[] = new char[length];
			int count;
			if (length > 0) {
				while ((count = inputStreamReader.read(buffer, 0, length)) > 0) {
					stringBuffer.append(buffer, 0, count);
				}
			}
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, e.getMessage());
		} catch (IllegalStateException e) {
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
		}
		return stringBuffer.toString();
	}
	
	String getEncoding(HttpEntity httpEntity){
		String value = httpEntity.getContentType().getValue();
		if(!TextUtils.isEmpty(value)){
			int startIndex = value.toLowerCase().indexOf("charset=");
			if(-1 != startIndex){
				startIndex += "charset=".length();
				int endIndex = value.toLowerCase().indexOf(";", startIndex);
				if(endIndex == -1)
					endIndex = value.length();
				String encoding = value.substring(startIndex, endIndex);
				if(!TextUtils.isEmpty(encoding))
					return encoding.trim();
			}
		}
		return HTTP.UTF_8;
	}

}